package team.first.movieapiv2.controller;

import cn.hutool.core.bean.BeanUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import team.first.movieapiv2.doc.RegionDoc;
import team.first.movieapiv2.domain.Genre;
import team.first.movieapiv2.domain.Region;
import team.first.movieapiv2.util.MongoDBUtil;
import team.first.movieapiv2.vo.ApiResponse;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.movieapiv2.controller
 * @Date: 2021/12/9 12:55
 * @Description:
 */
@RestController
public class RegionController {
    /**
     * 获取全部电影地区信息
     * 接口地址：GET /api/v2/regions
     */
    @ApiOperation("1.1 GET: 获取全部电影类型信息")
    @GetMapping("/api/v2/regions")
    public ApiResponse<List<Region>> getAllGenres() {
        ApiResponse<List<Region>> apiResponse = new ApiResponse<>();
        DBCollection collection = MongoDBUtil.getCollection("region");

        // 查询
        DBCursor cursor = collection.find().sort(new BasicDBObject("id", 1));

        // 将结果转为集合
        List<DBObject> dbObjectList = cursor.toArray();
        // 属性考呗
        List<Region> regionList = BeanUtil.copyToList(dbObjectList, Region.class);
        apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(regionList);
        return apiResponse;
    }
}
